/***************************************************************************************
Created By              : Sreekanth SV Vegi
Created on              : 02/21/2013
Version                 : 1.1
Description             : This is the test class for ConfirmAndCloseWCM
***************************************************************************************/
@isTest(seealldata=true)
public  class ConfirmAndCloseWCMTEST {




static    West_Case_Names__c   CreateTestWestCase()
{
    
    
   
        String queueName='CM - Queue 11';
    
   try
   
   {
    
   ////  Group objGroup= [SELECT Id, Name, Type FROM Group WHERE name =: queueName limit 1 ];
     
     // User lstUsersToDisplay = [Select Id, Name from User  where id=:userInfo.getUserId()];
    // GroupMember groupMembershipTobeAdded = new GroupMember();
      //  groupMembershipTobeAdded.GroupId = objGroup.id; 
     //    groupMembershipTobeAdded.UserOrGroupId =lstUsersToDisplay.Id;

         //   insert groupMembershipTobeAdded;
         
         
     Set<Id> setAssignedQueueIDs = new Set<Id>();
      for(Group objGroup: [SELECT Id, Name, Type FROM Group WHERE id in (SELECT GroupId FROM GroupMember WHERE userorgroupid = :userInfo.getUserId()) AND Type = 'Queue'])
      {
              setAssignedQueueIDs.add(objGroup.Id);
              
      }
       System.Debug('---STEP1-Available groups-'+  queueName);
          List<QueueSobject> lstQueueSobject=new List<QueueSobject>();
          lstQueueSobject = [Select Id, QueueId,Queue.Name, SobjectType 
                            from QueueSobject 
                            where Queue.Name Like '%CM - %' 
                            and sobjectType = 'Case'
                            and queueId IN :setAssignedQueueIDs ];
      
       
        queueName= lstQueueSobject[0].Queue.Name;
        System.Debug('---STEP2- available queuename-'+  queueName);
         
         
         
         
         
       }
       
       Catch(Exception Ex)
       {
           System.Debug('---exp--' +  Ex.getmessage());
       
       }
    West_Case_Names__c newwestcasename= new West_Case_Names__c(Name='Academic Probation11', West_Case_Name_Category__c='Academic', West_Case_Name_Activity__c='Program', 
                                                                 West_Case_Group__c='Administration', West_Queue_Name__c=queueName, West_Case_Priority__c='1', West_Case_Type_Deadline__c=8);
        insert newwestcasename;
       System.Debug('---STEP3- WestCaseName-'+  queueName);
         
     return   newwestcasename;

}  
    
   
   static testMethod void  AccountandleadBasedCases (){
   
   
        Test.startTest();
     
        String westProspectiveStudentRecordTypeId = Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('West - Prospective Student').getRecordTypeId();
        //lead Created 
        Lead lead=new Lead(FirstName='Test First Name',LastName='Test Lead',RecordTypeId = westProspectiveStudentRecordTypeId ,Status='New',LeadSource='ALUM',Country='India',Email='abc@df.com',Phone='9957890878',Home_Phone__c='9090909090');
        insert lead;
          System.Debug('---STEP0- Lead -');
        //West Case Name
       West_Case_Names__c   newRecord= CreateTestWestCase();
        
        
        
        
        String westSupportRecordTypeId = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId();
         System.Debug('---STEP4- -');
        list<Case>  Caselist=new list<Case>();
        
        Case  case1=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student1',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
        Case  case2=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student2',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
        Case  case3=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student3',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
        Case  case4=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student4',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
            System.Debug('---STEP5- -');
        
          Caselist.add(case1);
          Caselist.add(case2);
          Caselist.add(case3);
         Caselist.add(case4);
        //  Caselist.add(case5);
         
         insert Caselist;
         
         System.Debug('---STEP6- -');
         
         System.debug('Cases Count==========================='+ case1.lead__c);
          ApexPages.currentPage().getParameters().put('accid',null);
           ApexPages.currentPage().getParameters().put('leadid',case1.lead__c);
         
      
         ConfirmAndCloseCaseForWCM Closedlist=new ConfirmAndCloseCaseForWCM();
         
          
         Closedlist.leadid=case1.lead__c;     //'00QL0000001zgn8';
         Closedlist.accid=null;
         
         Closedlist.init();
         System.Debug('size1====' +  Closedlist.getcases().Size());
         
          System.Debug('---STEP7- getcases -' );
        Caselist= Closedlist.getCases();
       
        
        
        
         // Integer start = System.Now().millisecond();
     //  while(System.Now().millisecond()< start+1000){ 
       
      // }
         // Caselist.add(case5);
          Case  case5=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student5',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
         
          insert case5;
             System.Debug('---STEP7-init');
          Closedlist.init();
            System.Debug('---STEP8-getcases');
        Caselist= Closedlist.getCases();
        
             
       Closedlist.ConfirmAndClose();  
       Caselist[0].notes__c='Test';
       Caselist[1].notes__c='Test';
       Caselist[2].notes__c='Test';
       Caselist[3].notes__c='Test';
       Caselist[4].notes__c='Test';
       Caselist[0].Next_Call__c=System.now()+20;
        Caselist[1].Next_Call__c=System.now()+20;
       Caselist[2].Next_Call__c=System.now()+20;
       Caselist[3].Next_Call__c=System.now()+20;
       Caselist[4].Next_Call__c=System.now()+20;
       
        Caselist[0].Status='None';
         System.Debug('---STEP8--Confirm & close');
          Closedlist.ConfirmAndClose();
            Caselist[0].Status='Closed';   
        Caselist[1].Status='On Hold (Student)'; 
       Caselist[2].Status='On Hold (Advisor)'; 
       Caselist[3].Status='Escalated'; 
      Caselist[0].Status='Closed'; 
        Closedlist.ConfirmAndClose();  
         System.Debug('---STEP9--Confirm & close');
      
         Closedlist.doRefresh();
                 Test.stopTest();
                 
                 
                 
           }
       
         
         
         
   static testMethod void  AccountBasedCases (){
   
   
        Test.startTest();
        
            /****Creating Test Database****/
        Account testAccount = TestDatabaseCMS.createAccountForTesting();
        Contact testcontact=new Contact(account= testAccount,lastname='Testcontact',email='test@test.com');
        insert testcontact;
        String westProspectiveStudentRecordTypeId = Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('West - Prospective Student').getRecordTypeId();
        //lead Created 
        Lead lead=new Lead(FirstName='Test First Name',LastName='Test Lead',RecordTypeId = westProspectiveStudentRecordTypeId ,Status='New',LeadSource='ALUM',Country='India',Email='abc@df.com',Phone='9957890878',Home_Phone__c='9090909090');
        insert lead;
        
        //West Case Name
         West_Case_Names__c  newRecord= CreateTestWestCase();
        
        
        
        
        String westSupportRecordTypeId = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId();
        
        list<Case>  Caselist=new list<Case>();
        
        Case  case1=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student1',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
        case1.Account=testAccount;
        Case  case2=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student2',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
         case2.Account=testAccount;
        Case  case3=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student3',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
         case3.Account=testAccount;
        Case  case4=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student4',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
         case4.Account=testAccount;
       
        
          Caselist.add(case1);
          Caselist.add(case2);
          Caselist.add(case3);
         Caselist.add(case4);
        //  Caselist.add(case5);
         
         insert Caselist;
         
      //    ApexPages.currentPage().getParameters().put('accid',   case1.Account.id);
       //   ApexPages.currentPage().getParameters().put('leadd', case1.lead__c);
         System.debug('Cases Count==========================='+ case1.lead__c);
      
         
      
         ConfirmAndCloseCaseForWCM Closedlist=new ConfirmAndCloseCaseForWCM();
         
         
         Closedlist.leadid=case1.lead__c;     //'00QL0000001zgn8';
       //  Closedlist.accid=case1.Account.id;//
         
         Closedlist.init();
         System.Debug('size1====' +  Closedlist.getcases().Size());
         
         
        Caselist= Closedlist.getCases();
       
        
        
        
         // Integer start = System.Now().millisecond();
     //  while(System.Now().millisecond()< start+1000){ 
       
      // }
         // Caselist.add(case5);
          Case  case5=new Case(Lead__c=lead.id,RecordTypeId = westSupportRecordTypeId ,ownerId = userInfo.getUserID(),Case_Name__c=newRecord.Name ,Status='New',Origin='Web',Subject='This is test case by student5',Description='This is test desctiption by student',Next_Call__c=(System.now()-20));
          case5.Account=testAccount;
          insert case5;
        Closedlist.ConfirmAndClose();
          Closedlist.init();
        Caselist= Closedlist.getCases();
        
             
       Closedlist.ConfirmAndClose();  
       Caselist[0].notes__c='Test';
       Caselist[1].notes__c='Test';
       Caselist[2].notes__c='Test';
       Caselist[3].notes__c='Test';
       Caselist[4].notes__c='Test';
       Caselist[0].Next_Call__c=System.now()+20;
        Caselist[1].Next_Call__c=System.now()+20;
       Caselist[2].Next_Call__c=System.now()+20;
       Caselist[3].Next_Call__c=System.now()+20;
       Caselist[4].Next_Call__c=System.now()+20;
       
        Caselist[0].Status='None';
        
          Closedlist.ConfirmAndClose();
            Caselist[0].Status='Closed';   
        Caselist[1].Status='On Hold (Student)'; 
       Caselist[2].Status='On Hold (Advisor)'; 
       Caselist[3].Status='Escalated'; 
      Caselist[0].Status='Closed'; 
        Closedlist.ConfirmAndClose();  
           
      
         Closedlist.doRefresh();
                 Test.stopTest();
                 
                 
                 
           }
         }